//
//  APBlockListViewController.m
//  AqualPolo
//
//  Created by dong on 2024/5/9.
//

#import "APBlockListViewController.h"
#import "APAuthorTableViewCell.h"
#import "APBlockUserModel.h"

@interface APBlockListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UILabel *emptyLabel;

@end

@implementation APBlockListViewController
- (UILabel *)emptyLabel{
    if (!_emptyLabel) {
        _emptyLabel = [[UILabel alloc] init];
        _emptyLabel.textColor = LYRGBAColor(255, 255, 255, 0.6);
        _emptyLabel.font = [LYFont fontWithSize:12 fontName:LYFontNamepfr];
        _emptyLabel.textAlignment = NSTextAlignmentLeft;
        _emptyLabel.text = @"No Data";
        [self.view addSubview:_emptyLabel];
        [_emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.view);
        }];
    }
    return _emptyLabel;
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 76;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorColor = UIColor.clearColor;
        [_tableView registerClass:[APAuthorTableViewCell class] forCellReuseIdentifier:@"item"];
        _tableView.backgroundColor = UIColor.clearColor;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self loadNaviBar];
    self.naviBar.titleLabel.text = @"Block List";
    [self initUI];
    _tableView.mj_footer = [APRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page += 1;
        [self requestDataList];
    }];
    _tableView.mj_header = [APRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self.dataArray removeAllObjects];
        [self requestDataList];
    }];
    [self requestDataList];
}

-(void)requestDataList {
    [self.view ShowProgress];
    [APDataManager postJsonWithUrlString:blockList timeoutInterval:0 parameters:@{@"limit": @20, @"page": @(_page)} result:^(NSDictionary * _Nullable res) {
        [self.view removeProgress];
        if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }
        for (NSDictionary *dict in res[@"data"]) {
            APBlockUserModel *model = [APBlockUserModel yy_modelWithDictionary:dict];
            [self.dataArray addObject:model];
        }
        self.emptyLabel.hidden = self.dataArray.count != 0;
        [self.tableView reloadData];
    } failBlock:^(NSString * _Nullable errorMsg) {
        [self.view ShowProgressOnlyLabel:errorMsg];
    }];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    APAuthorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"item"];
    APBlockUserModel *model = [self.dataArray objectAtIndex:indexPath.row];
    [cell.avatarImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    cell.nameLabel.text = model.nickName;
    cell.contentLabel.text = [NSString stringWithFormat:@"%zd%@",model.age,model.registerCountry];
    [cell.unfollowButton setTitle:@"Unblock" forState:UIControlStateNormal];
    [cell.unfollowButton addTarget:self action:@selector(unfollowButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)unfollowButtonDidClick:(UIButton *)sender{
    CGPoint point = sender.center;
    point = [self.tableView convertPoint:point fromView:sender.superview];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
    APBlockUserModel *model = [self.dataArray objectAtIndex:indexPath.row];
    [self.view ShowProgress];
    [[APUserDataManager shareInstance] unBlockUser:model.broadcasterId result:^(_Bool success, NSString * _Nonnull msg) {
        if (success) {
            self.page = 1;
            [self.dataArray removeAllObjects];
            [self requestDataList];
        }else{
            [self.view ShowProgressOnlyLabel:msg];
        }
    }];
    
}

- (void)initUI{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.naviBar.mas_bottom).offset(12);
        make.left.right.bottom.equalTo(self.view);
    }];
}
@end
